/* File makes Table 7 */

/* 	Input files: RDQ_1019 (Compustat-crsp-mergedDB), rdq1020_cstat (Compustat), permno_symbol_match_final, temp_follows, crsp1019_ret
	Output files:  T7_Follow_xEAday.tex */

**********************************************
clear all
macro drop _all
scalar drive		= "E:"
scalar maindir		= "`=drive'\Replicate"
scalar tabledir		= "`=drive'\Replicate\TablesRep"
cd "`=maindir'"
**********************************************


use RDQ_1019, clear 
gen year=year(datadate)
keep if year==2019
ren lpermno permno
	drop if mi(rdq)
keep gvkey permno
gduplicates drop
save temp_rdq_gv, replace 

	use rdq1020_cstat, clear  
	drop if mi(rdq)
	keep gvkey datadate rdq
		gen year=year(datadate)
		keep if year==2020
		drop year datadate
	gduplicates drop 
	sum rdq, format
	joinby gvkey using temp_rdq_gv , unmatched(none)  
	sum rdq, format
	drop gvkey 
	gduplicates drop
	save temp_rdq_gv, replace  


use RDQ_1019, clear 
ren lpermno permno
	drop if mi(rdq)
keep permno rdq
append using temp_rdq_gv 
gduplicates drop

sort permno rdq
gen byte eann=1
ren rdq date

save rdq1020, replace
	erase temp_rdq_gv.dta

******************************************************************************


use permno symbolid using permno_symbol_match_final, clear 
merge 1:m permno using rdq1020
keep if _merge==3
drop _merge

replace date=date-1 

merge 1:m symbolid date using temp_follows 
drop if _merge==1
drop _merge
replace eann=0 if mi(eann) 



merge m:1 symbolid using permno_symbol_match_final, update 
gen year=year(date)
* tab year _merge
keep if _merge>=3
drop _merge year 

sort permno date
merge m:1 permno date using crsp1019_ret, update  
gen year=year(date)
* tab year _merge
keep if _merge>=3 
drop _merge

	
gen byte ea_pos =cond(eann==1 & xret_3day>0,1,0)
	
gen byte ea_neg =cond(eann==1 & xret_3day<=0,1,0)
	
keep symbolid permno date eann userid bull bear f_bull_t1t2 morebull_t1t2 morebear_t1t2 xret_3day ea*


save temp_eaday, replace

******************************************************************************


macro drop _all

use temp_eaday, clear

local lhsvar "f_bull_t1t2"

eststo N1: reghdfe `lhsvar' i.bull, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bull]/avg)*100

eststo N2: reghdfe `lhsvar' i.bull##i.eann, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bull]/avg)*100

eststo X2: reghdfe `lhsvar' i.bull##i.ea_neg i.bull##i.ea_pos, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bull]/avg)*100


local lhsvar "morebull_t1t2"

eststo N3: reghdfe `lhsvar' i.bull, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bull]/avg)*100

eststo N4: reghdfe `lhsvar' i.bull##i.eann , absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bull]/avg)*100

eststo N5: reghdfe `lhsvar' i.bull##i.ea_pos if eann==1, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bull]/avg)*100


local lhsvar "morebear_t1t2" 

eststo N6: reghdfe `lhsvar' i.bear, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bear]/avg)*100

eststo N7: reghdfe `lhsvar' i.bear##i.eann, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bear]/avg)*100

eststo N8: reghdfe `lhsvar' i.bear##i.ea_pos if eann==1, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bear]/avg)*100
	

******************************************************************************


cap estadd local USerFe "Y" : *
cap estadd local DsymbolFe "Y" : *

#delimit ; 
local stats_opts " stats(N N_clust r2 avg effect_size USerFe DsymbolFe, fmt(%18.0fc %18.0fc %12.2fc %12.2fc %12.0fc %12.0fc) labels("\# observations" "\# clusters (users)" "$\textit{R}^2$" "Mean of dependent var.(\%)" "Main effect size ($\%$ of mean)" "User FE" "Day x stock FE") ) " ;

local opts  "b(%16.2fc) se(%16.2fc) brackets booktabs  star(* 0.10 ** 0.05 *** 0.01) nonotes lines 
mtitles(
"Baseline" 
"\shortstack{$+$ EA day \\ interaction}"
"\shortstack{$+$ separate \\ EA day}"
"Baseline" 
"\shortstack{$+$ EA day \\ interaction}"
"\shortstack{Only \\ EA days}"
"Baseline" 
"\shortstack{$+$ EA day \\ interaction}"
"\shortstack{Only \\ EA days}"
)
mgroups("$\mathbbm{1}$ if follows\textsubscript{i,s,(t+1 $\rightarrow$ t+2)} are more Bull than Bear" "$\mathbbm{1}$ if net new \textit{Bull} follows\textsubscript{i,s,(t+1 $\rightarrow$ t+2)} >0" "$\mathbbm{1}$ if net new \textit{Bear} follows\textsubscript{i,s,(t+1 $\rightarrow$ t+2)} >0" , pattern(1 0 0 1 0 0 1 0 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  " ;   

local filename "T7_Follow_xEAday" ;

cd "`=tabledir'" ;
esttab N1 N2 X2 N3 N4 N5 N6 N7 N8 using `filename'.tex, `opts' `stats_opts' 
keep(1.bull 1.bull#1.eann 1.bull#1.ea_pos 1.bear 1.bear#1.eann 1.bear#1.ea_pos 1.bull#1.ea_neg) 
order (1.bull 1.bull#1.eann 1.bull#1.ea_pos 1.bull#1.ea_neg )
coef(1.bull "$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t}" 
1.bear 	"$\mathbbm{1}$ Declare Bear\textsubscript{i,s,t}"
1.bull#1.eann 	"$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t} x $\mathbbm{1}$ if EA day" 
1.bull#1.ea_pos "$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t} x $\mathbbm{1}$ if EA day$^\textbf{{+}}$" 
1.bull#1.ea_neg "$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t} x $\mathbbm{1}$ if EA day$^\textbf{{-}}$" 
1.bear#1.eann 	"$\mathbbm{1}$ Declare Bear\textsubscript{i,s,t} x $\mathbbm{1}$ if EA day" 
1.bear#1.ea_pos "$\mathbbm{1}$ Declare Bear\textsubscript{i,s,t} x $\mathbbm{1}$ if EA day$^\textbf{{+}}$" 
) replace  ;
#delimit cr 
cd "`=maindir'" 


